package project.ia04.agents.gui;

import jade.core.AID;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.gui.GuiAgent;
import jade.gui.GuiEvent;

import java.beans.PropertyChangeSupport;
import java.util.HashMap;

import project.ia04.gui.frames.StaffCommandDialog;
import project.ia04.utils.Data;

public class XCommandAgent extends GuiAgent {

	public static int STEP1 = 7;
	public static int STEP2 = 8;
	public static int STEP3 = 9;
	
	public static String[] cstXCommand2ConvId = new String[]{Data.STEP1,Data.STEP2,Data.STEP3};
	
	private int currType;
	private String staffName;
	
	public int getCurrType() {
		return currType;
	}

	public String getStaffName() {
		return staffName;
	}

	private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
	
	@Override
	protected void setup() {
		StaffCommandDialog frame = (StaffCommandDialog) getArguments()[0];
		frame.setAgent(this);
		pcs.addPropertyChangeListener(frame);
		currType = (Integer) getArguments()[1];
		staffName = (String)getArguments()[2];
		register();
		addBehaviour(new XCommandAgent2StudioAgentBehaviour());
	}

	private void register() {
		DFAgentDescription dfd = new DFAgentDescription();
		ServiceDescription sd = new ServiceDescription();
		sd.setType("IA04Project");
		sd.setName("XCommandAgent");
		dfd.addServices(sd);
		try {
			DFService.register(this, dfd);
		} catch (FIPAException e) {
			e.printStackTrace();
		}
	}
	
	public AID getEnvAgent(){
		DFAgentDescription dfd = new DFAgentDescription();
		ServiceDescription sd = new ServiceDescription();
		sd.setType("IA04Project");
		sd.setName("StudioAgent");
		dfd.addServices(sd);
		try {
			DFAgentDescription[] dfdArray = DFService.search(this, dfd);
			if(dfdArray.length >= 1)
				return dfdArray[0].getName();
		} catch (FIPAException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onGuiEvent(GuiEvent arg0) {
		
	}

	public void send2CmdDialog(HashMap<String, Integer> items) {
		pcs.firePropertyChange("UpdateItems", null,items);
	}
	
	public void CmdEnded(){
		pcs.firePropertyChange("Done", null, null);
	}


}
